package com.sgb.kjwl.view.ui.activity.manage.business

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.scwang.smart.refresh.layout.listener.ScrollBoundaryDecider
import com.sgb.businessmodule.entity.HomeBusinessListEntity
import com.sgb.businessmodule.view.ui.adapter.AdapterJumpPush
import com.sgb.businessmodule.view.ui.adapter.HomeBusinessAdapter
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivitySubscribeBusinessBinding
import com.sgb.kjwl.di.self.DaggerSelfComponent
import com.sgb.kjwl.di.self.SelfModule
import com.sgb.kjwl.viewmodel.manage.SubscribeBusinessViewModel
import com.swgk.core.base.di.AppComponent
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration
import javax.inject.Inject

/**
 * 订阅商机
 */
class SubscribeBusinessActivity : AppActivity() {

    private lateinit var mBinding : ActivitySubscribeBusinessBinding

    private var isLoadMore = false //是否加载更多
    private var isEnableLoadMore = false//是否能加载更多

    private lateinit var mAdapter : HomeBusinessAdapter

    @Inject
    lateinit var mViewModel : SubscribeBusinessViewModel

    companion object{
        fun launch(context: Context){
            val intent = Intent(context, SubscribeBusinessActivity::class.java)
            context.startActivity(intent)
        }
    }


    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
        DaggerSelfComponent.builder().appComponent(appComponent)
            .selfModule(SelfModule()).build().inject(this)
    }


    override fun initView() {
        super.initView()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_subscribe_business)
        mBinding.include.titleView.text = "订阅商机"
        mBinding.include.ivBack.setOnClickListener { finish() }
        initRecyclerView()
        mViewModel.businessList.observe(this, {
            isEnableLoadMore = it != null && (if (isLoadMore) it.size >= 10 else it.size >= 20)
            if (it != null){
                if (isLoadMore){
                    mAdapter.addDatas(it)
                } else {
                    if (it.isEmpty()){
                        mBinding.emptyView.visibility = View.VISIBLE
                    } else {
                        mBinding.emptyView.visibility = View.GONE
                    }
                    mAdapter.datas = it
                }
            }
        })
    }

    fun getData(){
        mViewModel.getSubscribeBusinessListData(isLoadMore)
    }

    override fun init() {
        super.init()
        isLoadMore = false
        getData()
    }

    @SuppressLint("UseRequireInsteadOfGet")
    private fun initRecyclerView() {
        // SwipeRefreshLayout 刷新
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                isLoadMore = true
                getData()
                mBinding.refreshLayout.finishLoadMore(500)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                isLoadMore = false
                getData()
                mBinding.refreshLayout.finishRefresh(500)
            }
        })
        //解决 在实现 上拉加载更多 和 下拉刷新 时  遇到item不在顶端就执行了刷新的方法和数据item没有填满屏幕也执行了加载更多
        mBinding.refreshLayout.setScrollBoundaryDecider(object : ScrollBoundaryDecider {
            override fun canRefresh(content: View): Boolean {
                if (mBinding.recycleView == null) return false
                return mBinding.recycleView.computeVerticalScrollOffset() === 0
            }

            override fun canLoadMore(content: View): Boolean {
                if (mBinding.recycleView == null) return false
                //整个View控件的高度
                val scrollRange: Int = mBinding.recycleView.computeVerticalScrollRange()
                //当前屏幕之前滑过的距离
                val scrollOffset: Int = mBinding.recycleView.computeVerticalScrollOffset()
                //当前屏幕显示的区域高度
                val scrollExtent: Int = mBinding.recycleView.computeVerticalScrollExtent()
                //获取recyclerView的高度
                val height: Int = mBinding.recycleView.height
                if (height > scrollRange) {
                    return false
                }
                return isEnableLoadMore && scrollRange <= scrollOffset + scrollExtent
            }
        })
        mBinding.recycleView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        mBinding.recycleView.setItemViewCacheSize(100)
        mBinding.recycleView.setHasFixedSize(true)
        mAdapter = HomeBusinessAdapter(this, ArrayList())
        mBinding.recycleView.adapter = mAdapter
        mBinding.recycleView.addItemDecoration(
            RecyclerCustomDecoration(
                0f,
                0f,
                0f,
                resources.getDimension(R.dimen.dp1), resources.getColor(R.color.color_main_bg)
            )
        )
        mAdapter.setOnItemClick(object : AdapterOnItemClick<HomeBusinessListEntity>() {
            override fun onItemClick(entity: HomeBusinessListEntity, position: Int) {
                AdapterJumpPush.getInstance(activity, entity) {
                    mAdapter.datas.remove(entity)
                    mAdapter.notifyDataSetChanged()
                }
            }
        })
    }

}